<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8"/>
		<title></title>
		<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js" type="text/javascript"></script>
		<script>
		 
		//加密
		function encrypt(str, pwd) {
			  if(pwd == null || pwd.length <= 0) {
			    alert("Please enter a password with which to encrypt the message.");
			    return null;
			  }
			  var prand = "";
			  for(var i=0; i<pwd.length; i++) {
			    prand += pwd.charCodeAt(i).toString();
			  }
			  var sPos = Math.floor(prand.length / 5);
			  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
			  var incr = Math.ceil(pwd.length / 2);
			  var modu = Math.pow(2, 31) - 1;
			  if(mult < 2) {
			    alert("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password.");
			    return null;
			  }
			  var salt = Math.round(Math.random() * 1000000000) % 100000000;
			  prand += salt;
			  while(prand.length > 10) {
			    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
			  }
			  prand = (mult * prand + incr) % modu;
			  var enc_chr = "";
			  var enc_str = "";
			  for(var i=0; i<str.length; i++) {
			    enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
			    if(enc_chr < 16) {
			      enc_str += "0" + enc_chr.toString(16);
			    } else enc_str += enc_chr.toString(16);
			    prand = (mult * prand + incr) % modu;
			  }
			  salt = salt.toString(16);
			  while(salt.length < 8)salt = "0" + salt;
			  enc_str += salt;
			  return enc_str;
			}

//解密


			function decrypt(str, pwd) {
			  if(str == null || str.length < 8) {
			    alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
			    return;
			  }
			  if(pwd == null || pwd.length <= 0) {
			    alert("Please enter a password with which to decrypt the message.");
			    return;
			  }
			  var prand = "";
			  for(var i=0; i<pwd.length; i++) {
			    prand += pwd.charCodeAt(i).toString();
			  }
			  var sPos = Math.floor(prand.length / 5);
			  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
			  var incr = Math.round(pwd.length / 2);
			  var modu = Math.pow(2, 31) - 1;
			  var salt = parseInt(str.substring(str.length - 8, str.length), 16);
			  str = str.substring(0, str.length - 8);
			  prand += salt;
			  while(prand.length > 10) {
			    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
			  }
			  prand = (mult * prand + incr) % modu;
			  var enc_chr = "";
			  var enc_str = "";
			  for(var i=0; i<str.length; i+=2) {
			    enc_chr = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255));
			    enc_str += String.fromCharCode(enc_chr);
			    prand = (mult * prand + incr) % modu;
			  }
			  return enc_str;
			}

		
		
		
			function setCookie(name,value)
			{
				var Days = 30;
				var exp = new Date();
				exp.setTime(exp.getTime() + Days*24*60*60*1000);
				document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
			}
			
			function getCookie(name)
			{
				var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
				if(arr=document.cookie.match(reg))
				return unescape(arr[2]);
				else
				return '';
			}
			
			
			function delCookie(name)
			{
			var exp = new Date();
			exp.setTime(exp.getTime() - 1);
			var cval=getCookie(name);
			if(cval!=null)
			document.cookie= name + "="+cval+";expires="+exp.toGMTString();
			}
			
			
			//token加密存入cookie，再从cookie取出来解密后作为令牌
			
			
			$(document).ready(function(){
				var cookie = getCookie('x-auth-token');
				if(cookie!=null&&cookie!=''){
				   var key =  decrypt(getCookie('x-auth-token'),'kuaige') ;
				}
				$('#login').click(function(){
					var form_data = {
					    'phone':$('#phone').val(),
					    'passWord':$('#passWord').val()
                    };
					
					$.ajax({
						type:'post',
						url:'http://127.0.0.1:8092/api/v1/wechat_service/admin/ajax_login',
						async: false,
						data:JSON.stringify(form_data),
						dataType:'json',
						contentType:'application/json;charest=utf-8',
		                beforeSend: function(request){
		                    request.setRequestHeader("x-auth-token",key);
		                },
		                success: function(result){
						    alert(result);
		                	if(result.code=='0'){
		                		setCookie('x-auth-token',encrypt(result.content.tokenFlag,'kuaige'));
		                		alert(result.content);
		                		delCookie('JSESSIONID');
		                    	location.href='http://127.0.0.1:8092/menu.html';
		                	} else{
		                		alert(result.msg);
		                	    location.href='login.html';
		                	}
		                }, error: function(XMLHttpRequest, textStatus, errorThrown) {
		                	 alert(XMLHttpRequest.status);
		                	 alert(XMLHttpRequest.readyState);
		                	 alert(textStatus);
		               }
					});
				});
			});
		</script>
	</head>
	<body>
		<input id="phone" type="text" /><br/>
		<input id="passWord" type="passWord" />
		<div id="login" style="cursor: pointer;width: 300px;line-height: 50px;">登录</div>
	</body>
</html>
